from django.contrib.sitemaps import Sitemap
from django.contrib.flatpages.models import FlatPage
from coresales.models import Category, Catalog, Product
from django.contrib.sitemaps import ping_google

class CategorySitemap(Sitemap):
        changefreq = "monthly"
        priority = 0.6 
        def items(self):
                return Category.objects.all()
        def location(self, obj):
                return '/' + "categoria_"+ obj.slug + "__catalogo_" + obj.catalog.slug 
              
class CatalogSitemap(Sitemap):
        changefreq = "never"
        priority = 0.5 
        def items(self):
                return Catalog.objects.all()
        def location(self, obj):
                return '/' + "catalogo_"+ obj.slug
        
class FlatpageSitemap(Sitemap):
    def items(self):
        return FlatPage.objects.all()
    def location(self, obj):
        return obj.url

class ItemSitemap(Sitemap):
        changefreq = "weekly"
        priority = 0.7 
        def items(self):
                return Product.objects.all()
        def location(self, obj):
                return '/articulo_' + obj.slug + "__categoria_"+ obj.category.slug + "__catalogo_" + obj.category.catalog.slug 
    
def ping_all_search_engines(sitemap_url=None):
    """
    Pings the popular search engines, Google, Yahoo, ASK, and
    Windows Live, to let them know that you have updated your
    site's sitemap. Returns successfully pinged servers.
    """
    SEARCH_ENGINE_PING_URLS = (
        ('google', 'http://www.google.com/webmasters/tools/ping'),
        ('yahoo', 'http://search.yahooapis.com/SiteExplorerService/V1/ping'),
        ('ask', 'http://submissions.ask.com/ping'),
        ('live', 'http://webmaster.live.com/ping.aspx'),
    )
    successfully_pinged = []
    for (site, url) in SEARCH_ENGINE_PING_URLS:
        try:
            ping_google(sitemap_url=sitemap_url, ping_url=url)
            pinged = True
        except:
            pinged = False
        if pinged:
            successfully_pinged.append(site)
    return successfully_pinged
        
